package com.auto.learning.wxapi;

import java.sql.SQLException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.MyMoneyActivity;
import com.auto.learning.activity.SubscriptionActivity;
import com.auto.learning.base.BaseActivity;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.bean.UserVipInfo;
import com.auto.learning.event.BuySuccessEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.pay.UserBuyUtils;
import com.auto.learning.pay.wechatpay.WechatPayCommon;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.MyToast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

	private IWXAPI api;

	TextView pay_result;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_result);
		setTitle("支付结果");
		pay_result = (TextView) findViewById(R.id.pay_result);
		api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
		api.handleIntent(getIntent(), this);
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				finish();
			}
		}, 1500);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		
	}



	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			String msg = "";
			switch (resp.errCode) {
				case 0: //成功
					msg = "支付成功";
					//是不是需要购买书籍
					if(TemporaryVariable.bookmoney.getType()!=0){
						new UserBuyUtils(WXPayEntryActivity.this,TemporaryVariable.bookmoney, 6).tomoney();
					}
					UserInfoBean userInfoBean = MyUserInfo.getInstance().getUserInfoBean();
					if (userInfoBean.getVipInfo() != null) {
						UserVipInfo userVipInfo = userInfoBean.getVipInfo();
						userVipInfo.setBalanceFen(userVipInfo.getBalanceFen() + WechatPayCommon.payMoney);
						userVipInfo.setBalanceYuan(CommonMethod.calculationMoney(userVipInfo.getBalanceFen()));
						MyUserInfo.getInstance().getUserInfoBean().setVipInfo(userVipInfo);
					}
					EventBus.getEventBus("weixin_recharge_success").post(WechatPayCommon.payMoney);
					break;
				case -1: //错误
				case -2:  //用户取消
					msg = "支付失败";
					break;
			}
			if (pay_result != null) {
				pay_result.setText(msg);
			}
			WechatPayCommon.payMoney = 0;
			CommonMethod.makeNotice(this, msg);
		}
	}

	@Override
	protected void initContentView(Bundle savedInstanceState) {
		
	}

	@Override
	protected void handleMessage(Message msg) {
		
	}
}